import {
  Controller,
  Post,
  Get,
  Patch,
  Delete,
  Body,
  Param,
  Query,
} from '@nestjs/common';
import { ResourceService } from './resource.service';
import { CreateResourceDto } from './dto/create-resource.dto';
import { UpdateResourceDto } from './dto/update-resource.dto';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiQuery,
  ApiParam,
} from '@nestjs/swagger';
import { Public } from '../common/isPublic'; // 假设你有一个公共装饰器

@ApiTags('资源管理')
@Controller('resource')
export class ResourceController {
  constructor(private readonly resourceService: ResourceService) {}

  @Public() // 假设这是你的自定义装饰器，用于标记公共端点
  @Post()
  @ApiOperation({ summary: '创建资源' })
  @ApiResponse({ status: 201, description: '资源创建成功' })
  create(@Body() createResourceDto: CreateResourceDto) {
    return this.resourceService.create(createResourceDto);
  }

  @Public()
  @Post('exists')
  @ApiOperation({ summary: '检查资源是否存在' })
  @ApiParam({ name: 'fileName', description: '文件名称' })
  @ApiResponse({ status: 200, description: '检查结果返回' })
  exists(@Body('fileName') fileName: string) {
    return this.resourceService.exists(fileName);
  }

  @Get()
  @ApiOperation({ summary: '查询所有资源' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiResponse({
    status: 200,
    description: '资源列表返回成功',
    type: [CreateResourceDto],
  })
  findAll(@Query() query: { page?: number; limit?: number }) {
    return this.resourceService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '根据ID查询单个资源' })
  @ApiParam({ name: 'id', description: '资源ID' })
  @ApiResponse({ status: 200, description: '资源信息返回成功' })
  @ApiResponse({ status: 404, description: '资源不存在' })
  findOne(@Param('id') id: string) {
    return this.resourceService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新资源信息' })
  @ApiParam({ name: 'id', description: '资源ID' })
  @ApiResponse({ status: 200, description: '资源更新成功' })
  update(
    @Param('id') id: string,
    @Body() updateResourceDto: UpdateResourceDto,
  ) {
    return this.resourceService.update(id, updateResourceDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除资源' })
  @ApiParam({ name: 'id', description: '资源ID' })
  @ApiResponse({ status: 200, description: '资源删除成功' })
  @ApiResponse({ status: 404, description: '资源不存在' })
  remove(@Param('id') id: string) {
    return this.resourceService.remove(id);
  }
}
